package com.li.springbootproject.service.data;


import com.google.common.collect.Lists;
import com.li.springbootproject.domain.DataDto;
import com.li.springbootproject.domain.DataDtoTemp;
import com.li.springbootproject.mapper.DataDtoMapper;
import com.li.springbootproject.mapper.DataDtoTempMapper;
import com.lidong.toolkit.utils.ListUtils;
import com.lidong.toolkit.utils.UuidUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * @author LiDong
 * @version 1.0.0
 * @className DataServiceImpl.java
 * @createTime 2021年07月18日 15:51:00
 */
@Service
public class DataServiceImpl implements DataService {

    private static final Logger logger = LoggerFactory.getLogger(DataServiceImpl.class);


    @Autowired
    private DataDtoMapper dataDtoMapper;

    @Autowired
    private DataDtoTempMapper dataDtoTempMapper;

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public void insertDatas() {
        Random rand = new Random();
        String[] strArr = new String[]{
                "A1000", "B1000", "C1000", "D1000", "E1000", "AB1000", "AA1000", "CS1000", "AE1000", "AAA1000", "ADA1000", "ABC1000", "AAAA1000", "BAAAA1000",
        };
        List<DataDto> list = new ArrayList<>();
        long start = System.currentTimeMillis();
        for (int i = 0; i < 500000; i++) {
            int num = rand.nextInt(13);
            String uuid = String.valueOf(UuidUtils.simpleUuid());
            String code = "code" + i;
            String name = "name" + i;
            DataDto dataDto = new DataDto();
            dataDto.setId(uuid);
            dataDto.setDataCode(code);
            dataDto.setDataName(name);
            dataDto.setDataLocation("/" + uuid + "/" + code);
            dataDto.setRecordtime(new Date());
            dataDto.setType(String.valueOf(i));
            dataDto.setDescribe(String.valueOf(i));
            dataDto.setString1(strArr[num]);
            dataDto.setString2(strArr[num]);
            list.add(dataDto);
            if (list.size() == 10000) {
                logger.info("list 对象满 10000");
                for (List<DataDto> dataDtos : Lists.partition(list, 1000)) {
                    dataDtoMapper.insertBatch(dataDtos);
                }
                list.clear();
            }
        }
        if (!CollectionUtils.isEmpty(list)) {
            dataDtoMapper.insertBatch(list);
        }
        logger.info("总共耗时：{}", (System.currentTimeMillis() - start));
    }

    @Override
    public void insertDataTemp() {
        Random rand = new Random();
        String[] strArr = new String[]{
                "A1000", "B1000", "C1000", "D1000", "E1000", "AB1000", "AA1000", "CS1000", "AE1000", "AAA1000", "ADA1000", "ABC1000", "AAAA1000", "BAAAA1000",
        };
        List<DataDtoTemp> list = new ArrayList<>();
        for (int i = 0; i < 150000; i++) {
            int num = rand.nextInt(13);
            String uuid = String.valueOf(UuidUtils.simpleUuid());
            String code = "code" + i;
            String name = "name" + i;
            DataDtoTemp dataDto = new DataDtoTemp();
            dataDto.setId(uuid);
            dataDto.setCode(strArr[num] + i);
            list.add(dataDto);
            // if (list.size() == 10000) {
            //     logger.info("list 对象满 10000");
            //     for (List<DataDtoTemp> dataDtos : Lists.partition(list, 1000)) {
            //         dataDtoTempMapper.insertOrgTempBatch(dataDtos);
            //     }
            //     list.clear();
            // }
        }
        long start = System.currentTimeMillis();
        List<List<DataDtoTemp>> lists = ListUtils.splitList(list, 10000);
        for (List<DataDtoTemp> dataDtoTemps : lists) {
            dataDtoTempMapper.insertBatch(dataDtoTemps);
        }
        // if (!CollectionUtils.isEmpty(list)) {
        //     dataDtoTempMapper.insertOrgTempBatch(list);
        // }
        logger.info("总共耗时：{}", (System.currentTimeMillis() - start));
    }

    @Override
    public List<DataDto> getPageList() {
        List<DataDto> list = dataDtoMapper.select();
        List<DataDtoTemp> tempList = dataDtoTempMapper.select();
        return list;
    }
}
